using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    #region 单例
    public static Enemy Instance;
    private void Awake()
    {
        Instance = this;
    }
    #endregion

    public int enemyDeathCount = 0;    // 记录对方的死亡数
    // HP
    private int currHP;
    public int CurrHP
    {
        get => currHP;
        set
        {
            currHP = value;
            // 更新UI
            UIMain_Panel.Instance.UpdateEnemy_HP(CurrHP);
            if (enemyDeathCount == 0)
            {
                enemyDeathCount++;
            }
        }
    }

    // 对方手中的卡，顶多7张
    private List<CardDefine> currCardList = new List<CardDefine>(7);


    #region 卡牌的获取与清除
    /// <summary>
    /// 移除全部卡
    /// </summary>
    public void RemoveAllCard()
    {
        currCardList.Clear();
        // 更新UI
        UIMain_Panel.Instance.UpdateStandByCard_Enemy(currCardList.Count);
    }

    /// <summary>
    /// 获取卡
    /// </summary>
    public void GetCard(CardDefine card)
    {
        currCardList.Add(card);
        // 更新UI
        UIMain_Panel.Instance.UpdateStandByCard_Enemy(currCardList.Count);
    }
    #endregion


    #region 回合逻辑
    /// <summary>
    /// 进入主动回合
    /// </summary>
    public IEnumerator EnterRound()
    {
        while (!UIMain_Panel.Instance.IsMyTurn)
        {
            yield return null;
            if (Player.Instance.isWaitOther)
            {
                // TODO:需要一个网络卡牌define来记录双方玩家打出了什么牌
                //yield return Player.Instance.StartCoroutine(Player.Instance.EnterResponse());
            }
        }
        StopCoroutine(EnterRound());

    }

    /// <summary>
    /// 进入响应
    /// </summary>
    public IEnumerator EnterResponse(CardDefine card)
    {
        //TODO: 将来要卡住玩家的主动回合进程，不让玩家动
        while (Player.Instance.isWaitOther)
        {
            yield return null;
        }
        // 对方响应结束了，该回到我的主回合了
        StopAllCoroutines();
    }
    #endregion



    /// <summary>
    /// 使用卡牌
    /// </summary>
    public void UseCard(int index)
    {
        CardDefine card = currCardList[index];
        // 出卡特效
        UIMain_Panel.Instance.UserCard_Effect(card);
        // 如果有恢复能力
        if (card.Recover > 0)
        {
            CurrHP += card.Recover;
        }
        // 逻辑层面移除这张卡
        currCardList.RemoveAt(index);
        // 更新卡牌数量
        UIMain_Panel.Instance.UpdateStandByCard_Enemy(currCardList.Count);
    }
}
